//
//  MyselfHeaderView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/19.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyselfHeaderView.h"
#import <CommonCrypto/CommonDigest.h>
#import <JMessage/JMessage.h>
#import "ZJImageMagnification.h"

@interface MyselfHeaderView ()

@property (nonatomic, strong)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UIButton * editButton;
@property (nonatomic, weak)UIButton * moreButton;
@property (nonatomic, strong)UIImageView * genderImageView;
@property (nonatomic, strong)UIImageView * localImageView;
@property (nonatomic, strong)UILabel * infoLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UILabel * personality_signLabel;
@property (nonatomic, strong)UIView * topView;
@property (nonatomic, strong)UIButton * topLeftButton;
@property (nonatomic, strong)UIButton * topRightButton;
@property (nonatomic, strong)UIButton * leftView;
@property (nonatomic, strong)UIButton * rightView;
@property (nonatomic, strong)UIImageView * coinImageView;
@property (nonatomic, strong)UIImageView * orderImageView;
@property (nonatomic, strong)UIImageView * shopImageView;
@property (nonatomic, strong)UIImageView * integralImageView;
@property (nonatomic, weak)UILabel * integralLabel;
@property (nonatomic, weak)UILabel * numberLabel;
@property (nonatomic, weak)UILabel * amountLabel;
@property (nonatomic, weak)UILabel * moneyLabel;
@property (nonatomic, strong)UILabel * orderLabel;
@property (nonatomic, strong)UILabel * shopLabel;
@property (nonatomic, copy)NSString * money;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIButton * signButton;
//@property (nonatomic, weak)UIView * localView;


@end

@implementation MyselfHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    if ([dict[@"head_img"] isEqual:[NSNull null]]) {
        
        self.iconImageView.image = [UIImage imageNamed:@"组12"];
    }else{
        
        if([dict[@"head_img"] containsString:@"http"]) {
            
            NSURL * url = [NSURL URLWithString:dict[@"head_img"]];
            [self.iconImageView sd_setImageWithURL:url];
        } else {
            
            NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
            NSURL * url = [NSURL URLWithString:iconStr];
            [self.iconImageView sd_setImageWithURL:url];
        }
    }
    
    
    if ([dict[@"user_name"] isEqual:[NSNull null]]) {
        
        self.nameLabel.text = @"----";
    }else{
        
        self.nameLabel.text = dict[@"user_name"];
    }
    
    if ([dict[@"age"] isEqual:[NSNull null]]) {
        
        self.infoLabel.text = [NSString stringWithFormat:@"----"];
    }else{
        
        NSString * age = [NSString stringWithFormat:@"%@",dict[@"age"]];
        self.infoLabel.text = [NSString stringWithFormat:@"%@",age];
    }
    
    if ([dict[@"address"] isEqual:[NSNull null]]) {
        
        self.localLabel.text = @"----";
    }else{
        
        
        self.localLabel.text = dict[@"address"];
    }
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%@",dict[@"user_bill"]];
    self.money = [NSString stringWithFormat:@"%@",dict[@"user_bill"]];
    self.numberLabel.text = [NSString stringWithFormat:@"%@",dict[@"user_integral_number"]];
    
    if ([dict[@"personality_sign"] isEqual:[NSNull null]]) {
        
        self.personality_signLabel.text = @"快去添加个新签名吧~~";
    }else{
        
        
        self.personality_signLabel.text = dict[@"personality_sign"];
    }
    
    int sex = [dict[@"sex"] intValue];
    if (sex == 1) {
        
        self.genderImageView.image = [UIImage imageNamed:@"男性"];
    }else{
        
        self.genderImageView.image = [UIImage imageNamed:@"女性"];
    }

    int is_sign = [dict[@"is_sign"] intValue];
    
    if (is_sign == 1) {
        
        self.signButton.selected = YES;
    }else{
        
        self.signButton.selected = NO;
    }
}

- (void)setupUI{
    
    self.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    self.iconImageView = [self setupImageViewWithImage:@"组12"];
    self.iconImageView.layer.cornerRadius = 45 * SCREENH6;
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.userInteractionEnabled = NO;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGesture)];
//    [self.iconImageView addGestureRecognizer:tap];
    [self addSubview:self.iconImageView];
    
//    UIView * localView = [UIView new];
//    localView.backgroundColor = ColorWithRGB(0xEFF0F1);
//    [localView setAutoresizesSubviews:YES];
//    [localView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin];
//
//    self.localView = localView;
//
//    [self addSubview:self.localView];
    self.genderImageView = [self setupImageViewWithImage:@"masculine"];
    [self addSubview:self.genderImageView];
    self.localImageView = [self setupImageViewWithImage:@"placeholder"];
    [self addSubview:self.localImageView];
    
    UIButton * signButton = [UIButton new];
    
    [signButton setBackgroundImage:[UIImage imageNamed:@"个人中心签到"] forState:UIControlStateNormal];
    [signButton setTitle:@"签到" forState:UIControlStateNormal];
    [signButton setTitle:@"已签到" forState:UIControlStateSelected];
    [signButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    signButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [signButton addTarget:self action:@selector(clickSignInButton) forControlEvents:UIControlEventTouchUpInside];
    signButton.adjustsImageWhenHighlighted = NO;
    
    self.signButton = signButton;
    
    [self addSubview:self.signButton];
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.text = @"----";
    nameLabel.textColor = ZRBlackColor;
    nameLabel.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    nameLabel.userInteractionEnabled = NO;
    
    self.nameLabel = nameLabel;
    
    [self addSubview:self.nameLabel];
    
    UIButton * editButton = [UIButton new];
    
    [editButton setBackgroundColor:ZRBlackColor];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    editButton.titleLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    editButton.layer.cornerRadius = 5;
    editButton.layer.masksToBounds = YES;
    [editButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    editButton.hidden = YES;
    
    self.editButton = editButton;
    
    [self addSubview:self.editButton];
    
    self.infoLabel = [self setupLabelWithTitle:@"25" WithType:@"normal"];
    [self addSubview:self.infoLabel];
    self.localLabel = [self setupLabelWithTitle:@"Chengdu" WithType:@"normal"];
    [self addSubview:self.localLabel];
    
    self.personality_signLabel = [UILabel new];
    self.personality_signLabel.textColor = ZRBlackColor;
    self.personality_signLabel.text = @"----";
    self.personality_signLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.personality_signLabel.userInteractionEnabled = NO;
    self.personality_signLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.personality_signLabel];
    
//    self.topView = [self setupViewYellowButton];
    self.topView = [UIView new];
    self.topView.layer.cornerRadius = 8;
    self.topView.layer.masksToBounds = YES;
    self.topView.backgroundColor = ColorWithRGB(0xFFE055);
    [self addSubview:self.topView];
    self.leftView = [self setupButton];
    [self addSubview:self.leftView];
    self.rightView = [self setupButton];
    [self addSubview:self.rightView];
    self.topLeftButton = [UIButton new];
    [self.topLeftButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.topLeftButton];
    self.topRightButton = [UIButton new];
    [self.topRightButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:self.topRightButton];

    
    UIView * lineView = [UIView new];
    lineView.backgroundColor = ColorWithRGB(0xD8C55D);
    self.lineView = lineView;
    [self.topView addSubview:self.lineView];
    
    self.coinImageView = [self setupImageViewWithImage:@"余额-1"];
    [self.topView addSubview:self.coinImageView];
    self.integralImageView = [self setupImageViewWithImage:@"积分"];
    [self.topView addSubview:self.integralImageView];
    self.orderImageView = [self setupImageViewWithImage:@"coupon"];
    [self.leftView addSubview:self.orderImageView];
    self.shopImageView = [self setupImageViewWithImage:@"Myshop"];
    [self.rightView addSubview:self.shopImageView];
    
//    UIButton * moreButton = [UIButton new];
//
//    [moreButton setTitle:@"如何获得更多?" forState:UIControlStateNormal];
//    [moreButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
//    [moreButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
//    moreButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
//
//    self.moreButton = moreButton;
//
//    [self.topView addSubview:self.moreButton];
    
    UILabel * amountLabel = [UILabel new];
    
    amountLabel.text = @"我的余额";
    amountLabel.textColor = ZRBlackColor;
    amountLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.amountLabel = amountLabel;
    
    [self.topView addSubview:self.amountLabel];
    
    
    UILabel * moneyLabel = [UILabel new];
    
    moneyLabel.text = @"----";
    moneyLabel.textColor = ZRBlackColor;
    moneyLabel.font = [UIFont boldSystemFontOfSize:21 * SCREENH6];
    
    self.moneyLabel = moneyLabel;
    
    [self.topView addSubview:self.moneyLabel];
    
    
    UILabel * integralLabel = [UILabel new];
    
    integralLabel.text = @"我的积分";
    integralLabel.textColor = ZRBlackColor;
    integralLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.integralLabel = integralLabel;
    
    [self.topView addSubview:self.integralLabel];
    
    
    UILabel * numberLabel = [UILabel new];
    
    numberLabel.text = @"----";
    numberLabel.textColor = ZRBlackColor;
    numberLabel.font = [UIFont boldSystemFontOfSize:21 * SCREENH6];
    
    self.numberLabel = numberLabel;
    
    [self.topView addSubview:self.numberLabel];
    
    
    self.orderLabel = [self setupLabelWithTitle:@"我的订单" WithType:@"bottom"];
    [self.leftView addSubview:self.orderLabel];
    self.shopLabel = [self setupLabelWithTitle:@"我的店铺" WithType:@"bottom"];
    [self.rightView addSubview:self.shopLabel];
    
//    self.integralLabel.hidden = YES;
//    self.integralImageView.hidden = YES;
//    self.numberLabel.hidden = YES;
//    self.lineView.hidden = YES;
    
}

- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.width.height.mas_equalTo(90 * SCREENH6);
    }];
    
    [self.signButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self);
        make.top.mas_equalTo(self.iconImageView.mas_centerY);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.iconImageView);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
//    [self.localView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self);
//        make.height.mas_equalTo(15 * SCREENH6);
//        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7 * SCREENH6);
//    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel);
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(30 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.genderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7 * SCREENH6);
        make.height.width.mas_equalTo(13 * SCREENH6);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.localImageView);
        make.leading.mas_equalTo(self.genderImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.genderImageView);
        make.trailing.mas_equalTo(self.genderImageView.mas_leading).offset(-5 * SCREENH6);
    }];

    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.localLabel);
        make.trailing.mas_equalTo(self.localLabel.mas_leading).offset(-5 * SCREENH6);
        make.width.mas_equalTo(11 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.personality_signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.genderImageView.mas_bottom).offset(8 * SCREENH6);
        make.width.mas_equalTo(200 * SCREENH6);
    }];

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.height.mas_equalTo(94 * SCREENH6);
        make.top.mas_equalTo(self.genderImageView.mas_bottom).offset(35 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.topView);
        make.width.mas_equalTo(1 * SCREENH6);
        make.height.mas_equalTo(75 * SCREENH6);
    }];
    
    [self.topLeftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.mas_equalTo(self.topView);
        make.trailing.mas_equalTo(self.lineView.mas_leading);
    }];
    
    [self.topRightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.bottom.mas_equalTo(self.topView);
        make.leading.mas_equalTo(self.lineView.mas_trailing);
    }];
    
    [self.coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView).offset(35 * SCREENH6);
        make.centerY.mas_equalTo(self.topView);
        make.width.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.integralImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lineView.mas_trailing).offset(35 * SCREENH6);
        make.centerY.mas_equalTo(self.topView);
        make.width.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.coinImageView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.coinImageView.mas_bottom);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.amountLabel);
        make.centerY.mas_equalTo(self.coinImageView.mas_top);
    }];
    
    
    
    [self.integralLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.integralImageView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.integralImageView.mas_bottom);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.integralLabel);
        make.centerY.mas_equalTo(self.integralImageView.mas_top);
    }];
    
    
    
//    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.mas_equalTo(self.topView).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self.topView).offset(10 * SCREENH6);
//    }];

    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topView);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(8 * SCREENH6);
        make.width.mas_equalTo(168 * SCREENH6);
        make.height.mas_equalTo(65 * SCREENH6);
    }];
    
    [self.orderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.leftView).offset(30 * SCREENH6);
        make.centerY.mas_equalTo(self.leftView);
        make.width.height.mas_equalTo(37 * SCREENH6);
    }];
    
    [self.orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderImageView.mas_trailing).offset(8 * SCREENH6);
        make.centerY.mas_equalTo(self.leftView);
    }];

    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.leftView);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.orderImageView);
        make.leading.mas_equalTo(self.rightView).offset(30 * SCREENH6);
    }];
    
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopImageView.mas_trailing).offset(8 * SCREENH6);
        make.centerY.mas_equalTo(self.rightView);
    }];
}

- (UIButton *)setupButton{
    
    UIButton * button = [UIButton new];
    
    button.layer.cornerRadius = 8;
    button.layer.masksToBounds = YES;
    [button addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside
     ];
    [button setBackgroundColor:ZRWhiteColor];
    
    return button;
}

- (UIButton *)setupViewYellowButton{
    
    UIButton * backView = [UIButton new];
    
    backView.layer.cornerRadius = 8;
    backView.layer.masksToBounds = YES;
    backView.backgroundColor = ColorWithRGB(0xFFE055);
    [backView addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
   
    return backView;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = ZRBlackColor;
    
    if ([type isEqualToString:@"normal"]) {
    
        label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    }else{
        
        label.font = [UIFont systemFontOfSize:17 * SCREENH6];
    }

    return label;
}

- (UIImageView *)setupImageViewWithImage:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
//    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:image];;
    
    return imageView;
}


- (void)clickEditButton{
    
    
}

- (void)clickBottomButton:(UIButton *)button{
    
    NSString * name;
    
    if (button == self.rightView) {
        
        name = @"MyShop";
    }else if (button == self.leftView){
        
        name = @"HistoryOrder";
    }else if (button == self.editButton){
        
        name = @"edit";
    }else if (button == self.topLeftButton){
        
        name = @"money";
    }else if (button == self.topRightButton){
        
        name = @"inter";
    }
    
    if (self.myselfHeaderViewDelegate && [self.myselfHeaderViewDelegate respondsToSelector:@selector(clickItemButton: WithMoney:)])
    {
        [self.myselfHeaderViewDelegate clickItemButton:name WithMoney:self.money];
    }
}


- (void)clickSignInButton{
    
    if (!self.signButton.selected) {
       
        [[ZRNetworkTool sharedTool] signIntegralWithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                //                self.signButton.enabled = NO;
                
                if (self.myselfHeaderViewDelegate && [self.myselfHeaderViewDelegate respondsToSelector:@selector(updateData)])
                {
                    [self.myselfHeaderViewDelegate updateData];
                }
            }
        }];
    }
}

- (void)singleTapGesture{
    
//    [ZJImageMagnification scanBigImageWithImageView:self.iconImageView alpha:1];
}


@end
